package com.kelvin.oocl;

import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.kelvin.oocl.protocol.Protocol;

public class Client extends JFrame implements ActionListener{
	JTextField contentTf;
	JTextField sentTf;
	JButton sendBtn;
	JButton nameBtn;
	JButton grouBtn;
	JLabel label;
	
	Socket socket;
	BufferedReader reader;
	PrintWriter writer;
	ObjectOutputStream out;
	ObjectInputStream in;
	
	private String name;
	private String target;
	
	public Client() {
		init();
		// TODO Auto-generated constructor stub
	}
	public Client(Socket socket,String name) {
		this.socket = socket;
		this.name = name;
		init();
		addEvent();
		receiver();
		initResource();
		
	}
	
	private void receiver() {
		try {
			out = new ObjectOutputStream(socket.getOutputStream());
			in = new ObjectInputStream(socket.getInputStream());
		} catch (SecurityException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//监听信息
		new Thread() {

			@Override
			public void run() {
				try {
					while(true){
						System.out.println("ttttt");
						Protocol pro = (Protocol) in.readObject();
						if(pro.getType()==2){
							contentTf.setText(pro.getFrom()+"对你说："+pro.getContent());
						}else if(pro.getType()==1){
							contentTf.setText(pro.getFrom()+"init user：");
						}else if(pro.getType()==3){
							contentTf.setText(pro.getFrom()+"对大家说："+pro.getContent());
						}
					}
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}.start();
	}
	public void initResource(){
		try {
//			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//			writer = new PrintWriter(new BufferedOutputStream(socket.getOutputStream()));
			//模拟登陆
			Protocol protocol = new Protocol();
			protocol.setType(1);
			protocol.setFrom(this.name);
			out.writeObject(protocol);
			out.flush();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}

	private void init() {
		this.setSize(400, 350);
		this.setLayout(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		initComponent();
		this.setVisible(true);
	}

	private void addEvent() {
		sendBtn.addActionListener(this);
	}

	private void initComponent() {
		contentTf = new JTextField();
		sentTf = new JTextField();
		sendBtn = new JButton("chat");
		nameBtn = new JButton("who");
		nameBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				target = sentTf.getText();
			}
		});
		grouBtn = new JButton("group");
		grouBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String text = sentTf.getText();
				try {
					if(!"".endsWith(text)){
						Protocol protocol = new Protocol();
						protocol.setFrom(name);
						protocol.setType(3);
						protocol.setContent(text);
						out.writeObject(protocol);
						out.flush();
						contentTf.setText("你对大家说: "+protocol.getContent());
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
			}
		});
		label = new JLabel(this.name);
		
		label.setBounds(1, 1, 30, 25);
		contentTf.setBounds(10, 20, 300, 80);
		sentTf.setBounds(10, 100, 300, 80);
		sendBtn.setBounds(170, 180, 80, 30);
		nameBtn.setBounds(100, 180, 80, 30);
		grouBtn.setBounds(230, 180, 80, 30);
		
		this.add(contentTf);
		this.add(sentTf);
		this.add(sendBtn);
		this.add(nameBtn);
		this.add(grouBtn);
		this.add(label);
	}

	public void actionPerformed(ActionEvent e) {
		String text = sentTf.getText();
		try {
			if(!"".endsWith(text)){
				Protocol protocol = new Protocol();
				protocol.setFrom(this.name);
				protocol.setTo(this.target);
				protocol.setType(2);
				protocol.setContent(text);
				out.writeObject(protocol);
				out.flush();
				contentTf.setText("你对Jack说: "+protocol.getContent());
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
	}
	
	public static void main(String[] args) {
		new Client();
	}
	
}
